// owner-app/pages/subscribe-code/index.js
import { handlerGobackClick, hint, formatters } from "../../../utils/util";
const app = getApp();
import { Subscribe } from "../../../models/subscribe";
const subscribe = new Subscribe();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: "",
    id: "",
    sbucodeList: {},
    qrcodeJson: {},
    nowTime: "",
    numRefresh: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      nowTime: formatters(new Date(), "YY-MM-DD HH:mm:ss"),
    });
    //设置屏幕亮度
    wx.setScreenBrightness({
      value: 1, //屏幕亮度值，范围 0~1，0 最暗，1 最亮
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
    });

    this.initPageData();
  },
  handlerGobackClick,
  refresh() {
    this.initPageData();
  },
  initPageData() {
    subscribe.miniVisitorQueryById({ id: this.data.id }).then((res) => {
      if (res.code == 1000) {
        this.setData({
          qrcodeJson: res.result.qrcodeJson,
          sbucodeList: res.result,
          numRefresh: this.data.numRefresh + 1,
          nowTime: formatters(new Date(), "YY-MM-DD HH:mm:ss"),
        });
      }
    });

    if (this.data.numRefresh >= 1) {
      this.selectComponent("#qrcode").refresh();
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
